package com.gyj.user.controller;

import com.gyj.common.CommonResult;
import com.gyj.user.anno.MyCacheable;
import com.gyj.user.dao.UserDao;
import com.gyj.user.model.User;
import com.gyj.user.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Created by Gao on 2018/5/24.
 */
@Api(tags = "Redis注解相关的测试接口")
@RestController
@RequestMapping(value = "/Test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private UserDao userDao;

    private static final String sid = UUID.randomUUID().toString();

    @ApiOperation(value = "查询全部用户")
    @GetMapping("/getAllUserList")
    public List<User> findAll() {
        List<User> allUserList = testService.getAllUserList();

        return allUserList;
    }

    @ApiOperation(value = "新增测试数据")
    @GetMapping("/addUserList")
    public CommonResult<Boolean> addUserList() {
        CommonResult<Boolean> result = new CommonResult<Boolean>(true);
        Boolean b = testService.addUserList();
        if (b) {
            result.setSuccess(true);
            result.setMessage("添加成功！");
            return result;
        } else {
            result.setSuccess(false);
            result.setMessage("添加失败！");
            return result;
        }
    }

    @ApiOperation(value = "根据Id查找用户")
    @GetMapping("/getUserById")
    public User getUserById(@RequestParam("id") String id) {
        User user = testService.getUserById(id);

        return user;
    }

    @ApiOperation(value = "根据Id查找用户,可以批量")
    @GetMapping("/getUserByIds")
    public List<User> getUserByIds(@RequestParam("ids") List<String> ids) {
        List<User> list = new ArrayList<>();
       /* for (String id : ids) {
            list.add(testService.getUserById(id));
        }*/
        list.addAll(ids.stream().map(id -> testService.getUserById(id)).collect(Collectors.toList()));

        return list;
    }

    @ApiOperation(value = "根据Id查找用户, 自定义注解方式")
    @GetMapping("/getUserByIdAnno")
    @MyCacheable
    public User getUserByIdAnno(@RequestParam("id") String id) {
        User user = userDao.findById(Integer.parseInt(id));

        return user;
    }

    @ApiOperation(value = "更新用户基本信息,根据id和昵称")
    @GetMapping("/updateUser")
    public User updateUser(@RequestParam("id") String id, @RequestParam("nickName") String nickName) {
        User user = testService.updateUser(id, nickName);

        return user;
    }

    @ApiOperation(value = "根据id更新用户基本信息，测试数据")
    @GetMapping("/updateUserById")
    public User updateUserById(@RequestParam("id") String id) {
        User user = testService.updateUserById(id);

        return user;
    }

    @ApiOperation(value = "根据id删除用户基本信息")
    @GetMapping("/deleteUserById")
    public CommonResult<Boolean> deleteUserById(@RequestParam("id") String id) {
        CommonResult<Boolean> result = new CommonResult<Boolean>(true);
        testService.deleteUserById(id);
        result.setMessage("删除成功！");
        result.setSuccess(true);

        return result;
    }

    @ApiOperation(value = "根据id删除用户信息, 可批量删除")
    @GetMapping("/deleteUserByIds")
    public CommonResult<Boolean> deleteUserById(@RequestParam("ids") List<String> ids) {
        CommonResult<Boolean> result = new CommonResult<Boolean>(true);
        for (String id : ids) {
            testService.deleteUserById(id);
        }
        result.setMessage("删除成功！");
        result.setSuccess(true);

        return result;
    }

}
